﻿using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using SUNRPA.CONTROLS.IMPL.domain.flowRunningVar;
using System.Collections.Concurrent;

namespace SUNRPA.CONTROLS.IMPL.controls {
    public class ElseIFNAction:BaseControlActions {
        public override int Execute(int currentIndex, FlowItem item, ConcurrentDictionary<string, ConcurrentDictionary<string, RPAVariableDefineWrapper>> globleData, List<FlowItem> flowItems) {
            ElseIFNParam? param = item.ControlParam as ElseIFNParam;
            int conRelation = param.ConRelation;
            List<IFCondition> conditions = param.Conditions;
            bool conditionResult;
            if(conRelation == 0) { 
                //与
                bool resultTemp = true;
                foreach(IFCondition con in conditions) { 
                    resultTemp = resultTemp & con.GetConResult(globleData);
                    if(!resultTemp) { 
                        break;
                    }
                }
                conditionResult = resultTemp;
            } else { 
                //或
                bool resultTemp = false;
                foreach(IFCondition con in conditions) { 
                    resultTemp = resultTemp | con.GetConResult(globleData);
                    if(resultTemp) {
                        break;
                    }
                }
                conditionResult = resultTemp;
            }
            
            if(conditionResult) {
                for(int j = currentIndex + 1; j < item.EndTag.Index; j++) {
                    BaseControlActions.updateInfoDelegate(j);
                    j = ExecuteAction(flowItems[j], globleData, flowItems);
                }
                item.EndTag.Index = item.EndTag.EndTag.Index;
            }
            return item.EndTag.Index - 1;
        }
    }
}
